package com.example.demo.config;

import com.example.demo.constant.CacheConstant;
import jakarta.annotation.PostConstruct;
import org.springframework.data.redis.connection.stream.ReadOffset;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

@Component
public class StreamConsumerGroupInitializer {
    private final RedisTemplate<String, Object> redisTemplate;


    public StreamConsumerGroupInitializer(RedisTemplate<String, Object> redisTemplate) {
        this.redisTemplate = redisTemplate;
    }

    @PostConstruct
    public void initConsumerGroup() {
        try {
            redisTemplate.opsForStream().createGroup(CacheConstant.REDIS_STEAM_01, ReadOffset.from("0-0"), CacheConstant.GROUP_NAME_FDELAY);
        } catch (Exception e) {
            if (e.getCause().getMessage().contains("BUSYGROUP")) {
                System.out.println("消费者组已存在，无需重复创建");
            } else {
                throw e;
            }

        }
        try {
            redisTemplate.opsForStream().createGroup(CacheConstant.REDIS_STEAM_02, ReadOffset.from("0-0"), CacheConstant.GROUP_NAME_FJITTER);
        } catch (Exception e) {
            if (e.getCause().getMessage().contains("BUSYGROUP")) {
                System.out.println("消费者组已存在，无需重复创建");
            } else {
                throw e;
            }

        }
//        try {
//            redisTemplate.opsForStream().createGroup(CacheConstant.REDIS_STEAM_03, ReadOffset.from("0-0"), CacheConstant.GROUP_NAME_FTHROUGHPUT);
//        } catch (Exception e) {
//            if (e.getCause().getMessage().contains("BUSYGROUP")) {
//                System.out.println("消费者组已存在，无需重复创建");
//            } else {
//                throw e;
//            }
//
//        }
    }
}

